<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="36">
            <a-col :md="5" :sm="12">
              <a-form-item label="模块">
                <a-input v-model="queryParam.module" placeholder="请输入模块" style="width: 100%"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="操作类型">
                <a-select
                  placeholder="请选择操作类型"
                  v-model="queryParam.businessType"
                >
                  <a-select-option value="0">其它</a-select-option>
                  <a-select-option value="1">新增</a-select-option>
                  <a-select-option value="2">修改</a-select-option>
                  <a-select-option value="3">删除</a-select-option>
                  <a-select-option value="4">授权</a-select-option>
                  <a-select-option value="5">导出</a-select-option>
                  <a-select-option value="6">导入</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="请求方式">
                <a-select
                  placeholder="请选择请求方式"
                  v-model="queryParam.requestType"
                >
                  <a-select-option value="GET">GET</a-select-option>
                  <a-select-option value="POST">POST</a-select-option>
                  <a-select-option value="PUT">PUT</a-select-option>
                  <a-select-option value="DELETE">DELETE</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="7" :sm="18">
                <a-form-item label="操作时间">
                  <a-range-picker v-model="queryParam.createTime" @change="onChange" style="width: 100%"></a-range-picker>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="24">
                <a-form-item label="状态">
                  <a-select
                    placeholder="状态"
                    v-model="queryParam.status"
                  >
                    <a-select-option value="true">成功</a-select-option>
                    <a-select-option value="false">失败</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="12">
                <a-form-item label="操作人员">
                  <a-input v-model="queryParam.operator" placeholder="请输入操作人员" style="width: 100%"/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="3" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button style="margin-right: 15px" shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
                <a @click="toggleAdvanced" style="margin-left: 15px" >
                  <a-icon :style="{ fontSize: '20px',color: '#000000'}" :type="advanced ? 'fullscreen-exit' : 'fullscreen'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" :title="hiddenSearch ? '显示搜索' :'隐藏搜索'" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 30px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
        <a-button v-show="operateLogRemoveEnable && selectedRowKeys.length > 0" type="danger" shape="round" icon="delete" @click="onClick(selectedRowKeys)">删除</a-button>
        <a-button v-show="operateLogRemoveEnable" type="danger" shape="round" icon="delete" @click="removeAll">清空</a-button>
      </div>
      <s-table
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :rowSelection="operateLogRemoveEnable ? {selectedRowKeys: selectedRowKeys, onChange: onSelectChange} : null"
        showPagination="auto"
        :showTotal="true"
      >
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter"/>
        </span>
        <span slot="businessType" slot-scope="text">
          <a-tag :color="text | businessTypeColorFilter">
            {{ businessTypeFilter(text) }}
          </a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-dropdown :trigger="['click']">
              <a-menu slot="overlay" v-if="operateLogRemoveEnable">
                <a-menu-item v-show="operateLogRemoveEnable"><a @click="handleDelete(record.id)"><a-icon type="delete" theme="filled"/> 删除</a></a-menu-item>
                <a-menu-item><a @click="details(record)"><a-icon type="eye" theme="filled"/> 详情</a></a-menu-item>
              </a-menu>
              <a> <a-button type="dashed" icon="bars"/></a>
            </a-dropdown>
          </template>
        </span>
      </s-table>
      <operate-log-details ref="drawer"/>
    </a-card>
  </div>
</template>

<script>
import { STable } from '@/components'
import { checkPermission } from '@/utils/permissions'
import { findOperateLogPage, batchDelOperateLog, delAllOperateLog, delOneOperateLog } from '@/api/system'
import message from 'ant-design-vue/es/message'
import OperateLogDetails from '../../modules/OperateLogDetails'
const statusMap = {
  true: {
    status: 'success',
    text: '正常'
  },
  false: {
    status: 'error',
    text: '失败'
  }
}
const businessType = {
  0: {
    color: 'pink',
    text: '其它'
  },
  1: {
    color: 'green',
    text: '新增'
  },
  2: {
    color: 'orange',
    text: '修改'
  },
  3: {
    color: 'red',
    text: '删除'
  },
  4: {
    color: 'cyan',
    text: '授权'
  },
  5: {
    color: 'blue',
    text: '导出'
  },
  6: {
    color: 'purple',
    text: '导入'
  }
}
export default {
  components: {
    STable,
    OperateLogDetails
  },
  name: 'OperLog',
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      icon: 'eye-invisible',
      hiddenSearch: false,
      operateLogRemoveEnable: checkPermission('log:operateLog:remove'),
      selectedRowKeys: [],
      selectedRows: [],
      // 表头
      columns: [
        {
          title: '模块',
          dataIndex: 'module',
          width: 100,
          align: 'center'
        },
        {
          title: '标题',
          dataIndex: 'title',
          width: 100,
          align: 'left',
          ellipsis: true
        },
        {
          title: '操作类型',
          dataIndex: 'businessType',
          align: 'center',
          scopedSlots: { customRender: 'businessType' },
          width: 100
        },
        {
          title: '请求方式',
          dataIndex: 'requestType',
          align: 'center',
          width: 100
        },
        {
          title: '操作人',
          dataIndex: 'operator',
          align: 'center',
          width: 100
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center',
          width: 100
        },
        {
          title: '操作地点',
          dataIndex: 'operatorLocation',
          align: 'center',
          width: 100
        },
        {
          title: '操作IP',
          dataIndex: 'operatorIp',
          align: 'center',
          width: 100
        },
        {
          title: '操作时间',
          dataIndex: 'createTime',
          sorter: true,
          align: 'center',
          width: 200
        },
        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findOperateLogPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    },
    businessTypeColorFilter (type) {
      return businessType[type].color
    }
  },
  methods: {
    details (record) {
      this.$refs.drawer.details(record)
    },
    businessTypeFilter (type) {
      return businessType[type].text
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    onChange (date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1] + ' 23:59:59'
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    },
    handleDelete (id) {
      const self = this
      this.$confirm({
        title: '你确定删除该条操作日志记录吗?',
        onOk () {
          delOneOperateLog({ operateLogId: id }).then(res => {
            self.$refs.table.refresh()
            message.success('删除操作日志id为' + id + '的记录成功成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    },
    onClick (ids) {
      const self = this
      this.$confirm({
        title: '你确定删除该条操作日志记录吗?',
        content: h => <div style="color:red;">操作日志删除将永久不能恢复</div>,
        onOk () {
          batchDelOperateLog(ids).then(res => {
            self.$refs.table.refresh()
            message.success('操作日志删除成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    },
    removeAll () {
      const self = this
      this.$confirm({
        title: '你确定清空所有操作日志吗?',
        content: h => <div style="color:red;">这项操作,将删除所有操作日志,请谨慎操作</div>,
        onOk () {
          delAllOperateLog().then(res => {
            self.$refs.table.refresh()
            message.success('删除全部操作日志成功')
            self.selectedRowKeys = []
          })
        },
        onCancel () {
          self.selectedRowKeys = []
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
